package cn.itcast.bos.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
import cn.itcast.bos.web.action.common.BaseAction;
@ParentPackage("json-default")
@Namespace("/")
@Controller("standardAction")
@Scope("prototype")
public class StandardAction extends BaseAction<Standard> {
	private static final long serialVersionUID = 1L;
	
	
	//接收删除传递的ids参数
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}

	@Autowired
	private StandardService standardService;
	
	//保存standard
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String save(){
		System.out.println(model.getName());
		standardService.save(model);
		
		return SUCCESS;
	}
	//添加standard保存时异步校验name
	@Action(value="standard_vilidate",results={@Result(name="success",type="json")})
	public String vilidate(){
		String name = model.getName();
		List<Standard> list = standardService.findByName(name);
		if(list != null && list.size() != 1){
			ActionContext.getContext().getValueStack().push(true);
		}else{
			ActionContext.getContext().getValueStack().push(false);
		}
		return SUCCESS;
	}
	
	//批量删除standard
	@Action(value="standard_delete",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String delete(){
		String[] idsArr = ids.split(",");
		standardService.delete(idsArr);
		
		
		return SUCCESS;
	}
	
	
	
	
	//分页查询standard pageable page-1  rows  total rows 
	@Action(value="standard_findPage",results={@Result(name="success", type="json")})
	public String findPage(){
		//pageable 用户 jparepository  findAll(pageable),构造pageable子类  pageRequest 参数为  page(第几页) 和 size(每页显示数量)
		Pageable pageable = new PageRequest(page-1, rows);//page从0查起,所以参数传递为page-1
		Page<Standard> page  = standardService.findPage(pageable);
		
		pushPageToValueStack(page);
		return SUCCESS;
	}
	
	//快递员收派标准回显
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> list = standardService.findAll();
		ActionContext.getContext().getValueStack().push(list);
		
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
}
